/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bc;

import edu.upn.sigemfi.seg.bc.PersonaNaturalFacadeLocal;
import edu.upn.sigemfi.seg.be.PersonaNatural;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author GABRIEL
 */
public class PersonaNaturalDelegate {

    private PersonaNaturalFacadeLocal local;

    public PersonaNaturalDelegate() {
        local = lookupPersonaNaturalFacade();
    }

      public PersonaNatural buscar(PersonaNatural personaNatural) throws Exception {
        try {
            return local.find(personaNatural.getIdPersona());
        } catch (Exception e) {
            local = lookupPersonaNaturalFacade();
            throw new Exception("Buscar PersonaNatural. " + e.getMessage(), e);
        }
    }
    
    public void crear(PersonaNatural persona) throws Exception{
        
        try{
            local.create(persona);
        }catch(Exception ex){
            local = lookupPersonaNaturalFacade();
            throw new Exception("Crear Persona... " + ex.getMessage());
        }
        
    }

    public PersonaNatural buscarPersonaPorDni(String dni) throws Exception{
        PersonaNatural persona = null;
        try{
            persona = local.findByDni(dni);
        }catch(Exception ex){
            local = lookupPersonaNaturalFacade();
            throw new Exception("Buscar Persona por DNI... " + ex.getMessage());
        }
        
        return persona;
    }

    private PersonaNaturalFacadeLocal lookupPersonaNaturalFacade() {
        try {
            Context c = new InitialContext();
            return (PersonaNaturalFacadeLocal) c.lookup("java:comp/env/PersonaNaturalFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }



}

